home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / rkeyboar.cpt / Reactive Keyboard ƒ / utl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-23  |  60.2 KB  |  2,037 lines

  1. /*______________________________________________________________________
  2.  
  3.     utl.c - Utilities.
  4.     
  5.     Copyright ⌐ 1988, 1989, 1990 Northwestern University.  Permission is granted
  6.     to use this code in your own projects, provided you give credit to both
  7.     John Norstad and Northwestern University in your about box or document.
  8.     
  9.     This module exports miscellaneous reusable utility routines.
  10. _____________________________________________________________________*/
  11.  
  12.  
  13. #include "MacIncludes.h"
  14. #include "utl.h"
  15. #include "Rk.h"
  16.  
  17. #pragma segment utl
  18.  
  19.  
  20. /*______________________________________________________________________
  21.  
  22.     Global Variables.
  23. _____________________________________________________________________*/
  24.  
  25.  
  26. static SysEnvRec        TheWorld;        /* system environment record */
  27. static Boolean            GotSysEnviron = false;    
  28.                                                 /* true if sys environ has been
  29.                                                     gotten */
  30.     
  31.             
  32. static CursHandle     *CursArray;        /* ptr to array of cursor handles */
  33. static short             NumCurs;            /* number of cursors to rotate */
  34. static short             TickInterval;    /* number of ticks between rotations */
  35. static short             CurCurs;            /* index of current cursor */
  36. static short             LastTick;        /* tick count at loast rotation */
  37.  
  38. static ModalFilterProcPtr    Filter;    /* dialog filter proc */
  39. static short            CancelItem;        /* item number of cancel button */
  40.  
  41. /*______________________________________________________________________
  42.  
  43.     GetSysEnvirons - Get System Environment.
  44.     
  45.     Exit:        global variable TheWorld = system environment record.
  46.                 global variable GotSysEnviron = true.
  47. _____________________________________________________________________*/
  48.  
  49.  
  50. static void GetSysEnvirons (void)
  51.  
  52. {
  53.     if (!GotSysEnviron) {
  54.         (void) SysEnvirons(curSysEnvVers, &TheWorld);
  55.         GotSysEnviron = true;
  56.     };
  57. }
  58.  
  59. /*______________________________________________________________________
  60.  
  61.     utl_AppendDITL - Append DITL to End of Dialog.
  62.     
  63.     Entry:    theDialog = pointer to dialog.
  64.                 theDITLID = rsrc id of DITL.
  65.                     
  66.     Exit:        function result = item number of first appended item.
  67.     
  68.     The dialog window is expanded to accomodate the new items, and the
  69.     new items are offset to appear at the bottom of the dialog.
  70.     
  71.     This routine is particularly useful for appending items to the
  72.     standard Page Setup and Print Job dialogs.  (See TN 95).  It was
  73.     written by Lew Rollins of Apple's Human-Systems Interface Group,
  74.     in MPW Pascal.  I translated it to MPW C.
  75.     
  76.     The only significant difference between this routine and Rollin's
  77.     version is that this version does not release the DITL resource.
  78. _____________________________________________________________________*/
  79.  
  80.  
  81. short utl_AppendDITL (DialogPtr theDialog, short theDITLID)
  82.  
  83. {
  84.     typedef struct DITLItem {
  85.         Handle            itmHndl;                /* handle or proc ptr */
  86.         Rect                itmRect;                /* display rect */
  87.         char                itmType;                /* item type */
  88.         unsigned char    itmData;                /* item data length byte */
  89.     } DITLItem;
  90.     
  91.     typedef struct itemList {
  92.         short                dlgMaxIndex;        /* num items - 1 */
  93.         DITLItem            DITLItems[1];        /* array of DITL items */
  94.     } itemList;
  95.     
  96.     short                offset;            /* item offset */
  97.     Rect                maxRect;            /* max dialog rect size so far */
  98.     Handle            hDITL;            /* handle to DITL */
  99.     DITLItem            *pItem;            /* pointer to item being appended */
  100.     itemList            **hItems;        /* handle to DLOG's item list */
  101.     short                sizeDITL;        /* size of DLOG's item list */
  102.     short                firstItem;        /* item num of first appended item */
  103.     short                newItems;        /* number of new items */
  104.     short                dataSize;        /* size of data for current item */
  105.     short                i;                    /* loop index */
  106.     
  107.     /* Initialize. */
  108.     
  109.     maxRect = theDialog->portRect;
  110.     offset = maxRect.bottom;
  111.     maxRect.bottom -= 5;
  112.     maxRect.right -= 5;
  113.     hItems = (itemList**)(((DialogPeek)theDialog)->items);
  114.     sizeDITL = GetHandleSize((Handle)hItems);
  115.     firstItem = (**hItems).dlgMaxIndex + 2;
  116.     hDITL = GetResource('DITL', theDITLID);
  117.     HLock(hDITL);
  118.     newItems = **(short**)hDITL + 1;
  119.     PtrAndHand(*hDITL+2, (Handle)hItems,
  120.         GetHandleSize(hDITL)-2);
  121.     (**hItems).dlgMaxIndex += newItems;
  122.     HUnlock(hDITL);
  123.     HLock((Handle)hItems);
  124.     (char*)pItem = (char*)(*hItems) + sizeDITL;
  125.     
  126.     /* Main loop.  Add each item to dialog item list. */
  127.     
  128.     for (i = 1; i <= newItems; i++) {
  129.         OffsetRect(&pItem->itmRect, 0, offset);
  130.         UnionRect(&pItem->itmRect, &maxRect, &maxRect);
  131.         switch (pItem->itmType & 0x7f) {
  132.             case ctrlItem+btnCtrl:
  133.             case ctrlItem+chkCtrl:
  134.             case ctrlItem+radCtrl:
  135.                 pItem->itmHndl = (Handle)NewControl(theDialog, 
  136.                     &pItem->itmRect, &pItem->itmData, true, 0, 0, 1, 
  137.                     pItem->itmType & 0x03, 0);
  138.                 break;
  139.             case ctrlItem+resCtrl:
  140.                 pItem->itmHndl = (Handle)GetNewControl(
  141.                     *(short*)(&pItem->itmData+1),
  142.                     theDialog);
  143.                 (**((ControlHandle)(pItem->itmHndl))).contrlRect = 
  144.                     pItem->itmRect;
  145.                 break;
  146.             case statText:
  147.             case editText:
  148.                 PtrToHand(&pItem->itmData+1, &(Handle)(pItem->itmHndl), 
  149.                     pItem->itmData);
  150.                 break;
  151.             case iconItem:
  152.                 pItem->itmHndl = GetIcon(*(short*)(&pItem->itmData+1));
  153.                 break;
  154.             default:
  155.                 pItem->itmHndl = nil;
  156.         };
  157.         dataSize = (pItem->itmData + 1) & 0xfffe;
  158.         (char*)pItem += dataSize + sizeof(DITLItem);
  159.     };
  160.     
  161.     /* Finish up. */
  162.     
  163.     HUnlock((Handle)hItems);
  164.     maxRect.bottom += 5;
  165.     maxRect.right += 5;
  166.     SizeWindow(theDialog, maxRect.right, maxRect.bottom, true);
  167.     return firstItem;
  168. }
  169.  
  170. /*______________________________________________________________________
  171.  
  172.     utl_CenterDlogRect - Center a dialog rectangle.
  173.     
  174.     Entry:    rect = rectangle.
  175.                 centerMain = true to center on main (menu bar) screen.
  176.                 centerMain = false to center on the screen containing
  177.                     the maximum intersection with the frontmost window.
  178.     
  179.     Exit:        rect = rectangle offset so that it is centered on
  180.                     the specified screen, with twice as much space below
  181.                     the rect as above.
  182.                     
  183.     See HIN 6.
  184. _____________________________________________________________________*/
  185.  
  186.  
  187. void utl_CenterDlogRect (Rect *rect, Boolean centerMain)
  188.  
  189. {
  190.     Rect        screenRect;            /* screen rectangle */
  191.     short        mBHeight;            /* menu bar height */
  192.     GDHandle    gd;                    /* gdevice */
  193.     Rect        windRect;            /* window rectangle */
  194.     Boolean    hasMB;                /* true if screen contains menu bar */
  195.  
  196.     mBHeight = utl_GetMBarHeight();
  197.     if (centerMain) {
  198.         screenRect = qd.screenBits.bounds;
  199.     } else {
  200.         utl_GetWindGD(FrontWindow(), &gd, &screenRect, &windRect, &hasMB);
  201.         if (!hasMB) mBHeight = 0;
  202.     };
  203.     OffsetRect(rect,
  204.         (screenRect.right + screenRect.left - rect->right - rect->left) >> 1,
  205.         (screenRect.bottom + ((screenRect.top + mBHeight - rect->top)<<1) - 
  206.             rect->bottom + 7) / 3);
  207. }
  208.  
  209. /*______________________________________________________________________
  210.  
  211.     utl_CenterRect - Center a rectangle on the main screen.
  212.     
  213.     Entry:    rect = rectangle.
  214.     
  215.     Exit:        rect = rectangle offset so that it is centered on
  216.                     the main screen.
  217. _____________________________________________________________________*/
  218.  
  219.  
  220. void utl_CenterRect (Rect *rect)
  221.  
  222. {
  223.     Rect        screenRect;            /* main screen rectangle */
  224.     short        mBHeight;            /* menu bar height */
  225.  
  226.     mBHeight = utl_GetMBarHeight();
  227.     screenRect = qd.screenBits.bounds;
  228.     OffsetRect(rect,
  229.         (screenRect.right + screenRect.left - rect->right - rect->left) >> 1,
  230.         (screenRect.bottom - screenRect.top + mBHeight - 
  231.             rect->bottom - rect->top) >> 1);
  232. }
  233.  
  234. /*______________________________________________________________________
  235.  
  236.     utl_CheckPack - Check to see if a package exists.
  237.     
  238.     Entry:    packNum = package number.
  239.                 preload = true to preload package.
  240.                     
  241.     Exit:        function result = true if package exists.
  242. _____________________________________________________________________*/
  243.  
  244.  
  245. Boolean utl_CheckPack (short packNum, Boolean preload)
  246.  
  247. {
  248.     short            trapNum;            /* trap number */
  249.     Handle        h;                    /* handle to PACK resource */
  250.     
  251.     /* Check to make sure the trap exists, by comparing its trap address to
  252.         the trap address of the unimplemented trap. */
  253.     
  254.     trapNum = packNum + 0x1e7;
  255.     if (NGetTrapAddress(trapNum & 0x3ff, ToolTrap) == 
  256.         NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap)) return false;
  257.         
  258.     /* Check to make sure the package exists on the System file or in 
  259.         ROM.  If it's not in ROM make it nonpurgeable, if requested. */
  260.     
  261.     if (preload) {
  262.         if (utl_Rom64()) {
  263.             h = GetResource('PACK', packNum);
  264.             if (!h) return false;
  265.             HNoPurge(h);
  266.         } else {
  267.             *(unsigned short*)RomMapInsert = 0xFF00;
  268.             h = GetResource('PACK', packNum);
  269.             if (!h) return false;
  270.             if ((*(unsigned long*)h & 0x00FFFFFF) < 
  271.                 *(unsigned long*)ROMBase) {
  272.                 h = GetResource('PACK', packNum);
  273.                 HNoPurge(h);
  274.             };
  275.         };
  276.         return true;
  277.     } else {
  278.         SetResLoad(false);
  279.         if (!utl_Rom64()) *(unsigned short*)RomMapInsert = 0xFF00;
  280.         h = GetResource('PACK', packNum);
  281.         SetResLoad(true);
  282.         if (h) return true; else return false;
  283.     };
  284. }
  285.  
  286. /*______________________________________________________________________
  287.  
  288.     utl_CopyPString - Copy Pascal String.
  289.     
  290.     Entry:    dest = destination string.
  291.                 source = source string.
  292. _____________________________________________________________________*/
  293.  
  294.  
  295. void utl_CopyPString (Str255 dest, Str255 source)
  296.  
  297. {
  298.     memcpy(dest, source, *source+1);
  299. }
  300.  
  301. /*______________________________________________________________________
  302.  
  303.     utl_CouldDrag - Determine if a window could be dragged to a location.
  304.     
  305.     Entry:    windRect = window rectangle, in global coords.
  306.                 offset = pixel offset used in DragRect calls.
  307.     
  308.     Exit:        function result = true if the window could have been
  309.                     dragged to the specified position.
  310.                     
  311.     This routine is used when restoring windows to saved positions.  According
  312.     to HIN 6, we must check to see if the window "could have been dragged to
  313.     the saved position."
  314.     
  315.     The "offset" parameter is usually 4.  When initializing the boundary rectangle
  316.     for DragWindow calls, normally the boundary rectangle of the desktop gray
  317.     region is inset by 4 pixels.  If some value other than 4 is used, it should
  318.     be passed to CouldDrag as the "offset" parameter.
  319.     
  320.     The algorithm used is the following:  The routine computes the four squares
  321.     at the corners of the title bar.  "true" is returned if and only if at least one 
  322.     of these four squares is completely contained within the desktop gray region.
  323.     
  324.     Three pixels are added to the offset to err on the side of requiring a larger
  325.     portion of the drag bar to be visible.  
  326. _____________________________________________________________________*/
  327.  
  328.  
  329. Boolean utl_CouldDrag (Rect *windRect, short offset)
  330.  
  331. {
  332.     RgnHandle            rgn;            /* scratch region handle */
  333.     Boolean                could;        /* function result */
  334.     short                    corner;        /* which corner */
  335.     Rect                    r;                /* corner rectangle */
  336.  
  337.     rgn = NewRgn();
  338.     could = false;
  339.     offset += 3;
  340.     for (corner = 1; corner <= 4; corner++) {
  341.         switch (corner) {
  342.             case 1:
  343.                 r.top = windRect->top - titleBarHeight;
  344.                 r.left = windRect->left;
  345.                 break;
  346.             case 2:
  347.                 r.top = windRect->top - offset;
  348.                 r.left = windRect->left;
  349.                 break;
  350.             case 3:
  351.                 r.top = windRect->top - titleBarHeight;
  352.                 r.left = windRect->right - offset;
  353.                 break;
  354.             case 4:
  355.                 r.top = windRect->top - offset;
  356.                 r.left = windRect->right - offset;
  357.                 break;
  358.         };
  359.         r.bottom = r.top + offset;
  360.         r.right = r.left + offset;
  361.         RectRgn(rgn, &r);
  362.         DiffRgn(rgn, *(RgnHandle*)GrayRgn, rgn);
  363.         if (EmptyRgn(rgn)) {
  364.             could = true;
  365.             break;
  366.         };
  367.     };
  368.     DisposeRgn(rgn);
  369.     return could;
  370. }
  371.  
  372. /*______________________________________________________________________
  373.  
  374.     utl_DILoad - Load Disk Initialization Package.
  375.     
  376.     Exit:        Disk initialization package loaded.
  377.                 
  378.     This routine is identical to the DILoad routine (see IM II-396),
  379.     except that it closes any resource files opened by the routine.  This is
  380.     necessary to undo a bug in the DaynaFile software.  DaynaFile patches
  381.     DILoad.  The patch opens a resource file without closing it.  The
  382.     effect on Sample if we don't do anything about this is that 
  383.     the DaynaFile icon is displayed in Sample's main window instead of
  384.     Sample's icon.  
  385. _____________________________________________________________________*/
  386.  
  387.  
  388. void utl_DILoad (void)
  389.  
  390. {
  391.     short            curResFile;            /* ref num of current resource file
  392.                                                 before calling DILoad */
  393.     short            topResFile;            /* ref num of top resource file
  394.                                                 after calling DILoad */
  395.     
  396.     curResFile = CurResFile();
  397.     DILoad();
  398.     while ((topResFile = CurResFile()) != curResFile) CloseResFile(topResFile);
  399. }
  400.  
  401. /*______________________________________________________________________
  402.  
  403.     utl_DoDiskInsert - Handle a disk inserted event.
  404.     
  405.     Entry:    message = message field from disk insertion event record
  406.                     = 16/MountVol return code, 16/drive number.
  407.     
  408.     Exit:        vRefNum = vol ref num of inserted volume.
  409.                 function result = error code.
  410.     
  411.     If MountVol returned an error code, the disk initialization package
  412.     is called to initialize the disk.
  413. _____________________________________________________________________*/
  414.  
  415.  
  416. OSErr utl_DoDiskInsert (long message, short *vRefNum)
  417.  
  418. {
  419.     OSErr                rCode;                /* result code */
  420.     short                driveNum;            /* drive number */
  421.     HParamBlockRec    pBlock;                /* vol info param block */
  422.     Handle            dlgHandle;            /* handle to disk init dialog */
  423.     Rect                dlgRect;                /* disk init dialog rectangle */
  424.     Point                where;                /* location of disk init dialog */
  425.     short                curVol;                /* index in VCB queue */
  426.     
  427.     /* Get result code and drive number from event message. */
  428.     
  429.     rCode = (message >> 16) & 0xffff;
  430.     driveNum = message & 0xffff;
  431.     
  432.     /* If the result code indicates an error, call DIBadMount to initialize
  433.         the disk. */
  434.         
  435.     if (rCode) {
  436.         
  437.         /* Center the disk initialization package dialog. */
  438.         
  439.         DILoad();
  440.         dlgHandle = GetResource('DLOG', -6047);
  441.         dlgRect = **(Rect**)dlgHandle;
  442.         utl_CenterDlogRect(&dlgRect, false);
  443.         SetPt(&where, dlgRect.left, dlgRect.top);
  444.         
  445.         /* Call DIBadMount. */
  446.         
  447.         if (rCode = DIBadMount(where, message)) return rCode;
  448.         
  449.     };
  450.     
  451.     /* Search mounted volumes to find inserted one. */
  452.     
  453.     pBlock.volumeParam.ioNamePtr = nil;
  454.     curVol = 0;
  455.     while (true) {
  456.         pBlock.volumeParam.ioVolIndex = ++curVol;
  457.         pBlock.volumeParam.ioVRefNum = 0;
  458.         if (rCode = PBHGetVInfo(&pBlock, false)) return rCode;
  459.         if (pBlock.volumeParam.ioVDrvInfo == driveNum) break;
  460.     };
  461.     *vRefNum = pBlock.volumeParam.ioVRefNum;
  462.     return noErr;
  463. }    
  464.  
  465. /*______________________________________________________________________
  466.  
  467.     utl_DrawGrowIcon - Draw Grow Icon.
  468.     
  469.     Entry:            theWindow = pointer to window.
  470.     
  471.     This routine is identical to the Window Manager routine 
  472.     DrawGrowIcon, except that it does not draw the lines enclosing the
  473.     scroll bars.
  474. _____________________________________________________________________*/
  475.  
  476.  
  477. void utl_DrawGrowIcon (WindowPtr theWindow) 
  478.  
  479. {
  480.     RgnHandle            clipRgn;            /* saved clip region */
  481.     Rect                    clipRect;        /* clip rectangle */
  482.     
  483.     clipRgn = NewRgn();
  484.     GetClip(clipRgn);
  485.     clipRect = theWindow->portRect;
  486.     clipRect.left = clipRect.right - 15;
  487.     clipRect.top = clipRect.bottom - 15;
  488.     ClipRect(&clipRect);
  489.     DrawGrowIcon(theWindow);
  490.     SetClip(clipRgn);
  491.     DisposeRgn(clipRgn);
  492. }
  493.  
  494. /*______________________________________________________________________
  495.  
  496.     utl_Ejectable - Test for ejectable volume.
  497.     
  498.     Entry:    vRefNum = volume reference number.
  499.     
  500.     Exit:        function result = true if volume is on an ejectable drive.
  501. _____________________________________________________________________*/
  502.  
  503.  
  504. Boolean utl_Ejectable (short vRefNum)
  505.     
  506. {
  507.     HParamBlockRec    pBlock;                /* vol info param block */
  508.     short                driveNum;            /* driver number of cur vol */
  509.     DrvQEl            *curDrive;            /* ptr to current drive queue element */
  510.     OSErr                rCode;                /* result code */
  511.     unsigned char    flagByte;            /* drive queue element flag byte */
  512.     
  513.     /* Get driveNum = drive number of drive containing volume. */
  514.     
  515.     pBlock.volumeParam.ioNamePtr = nil;
  516.     pBlock.volumeParam.ioVolIndex = 0;
  517.     pBlock.volumeParam.ioVRefNum = vRefNum;;
  518.     if (rCode = PBHGetVInfo(&pBlock, false)) return false;
  519.     driveNum = pBlock.volumeParam.ioVDrvInfo;
  520.     
  521.     /*    Walk the drive queue until we find driveNum.  The second byte in
  522.         the four flag bytes preceding the drive queue element is 8 or $48 
  523.         if the drive is nonejectable. */
  524.     
  525.     curDrive = (DrvQEl*)(GetDrvQHdr())->qHead;
  526.     while (true) {
  527.         if (curDrive->dQDrive == driveNum) {
  528.             flagByte = *((Ptr)curDrive - 3);
  529.             return (flagByte != 8 && flagByte != 0x48);
  530.         };
  531.         curDrive = (DrvQEl*)curDrive->qLink;
  532.         if (!curDrive) return false;
  533.     };
  534. }
  535.  
  536. /*______________________________________________________________________
  537.  
  538.     utl_FixStdFile - Fix Standard File Pacakge.
  539.     
  540.     This routine should be called before calling the Standard File 
  541.     package if there's any chance that SFSaveDisk might specify
  542.     a volume that has been umounted.  Standard File gets confused if this
  543.     happens and presents an alert telling the user that a "system error"
  544.     has occurred.
  545.     
  546.     This routine checks to make sure that SFSaveDisk specifies a volume 
  547.     that is still mounted.  If not, it sets it to the first mounted 
  548.     volume, and it sets CurDirStore to the root directory on that volume.
  549. _____________________________________________________________________*/
  550.                     
  551.                     
  552. void utl_FixStdFile (void)
  553.  
  554. {
  555.     ParamBlockRec    vBlock;            /* vol info param block */
  556.                                                 
  557.     vBlock.volumeParam.ioNamePtr = nil;
  558.     vBlock.volumeParam.ioVRefNum = -*(short*)SFSaveDisk;
  559.     vBlock.volumeParam.ioVolIndex = 0;
  560.     if (PBGetVInfo(&vBlock, false)) {
  561.         vBlock.volumeParam.ioVolIndex = 1;
  562.         (void) PBGetVInfo(&vBlock, false);
  563.         *(short*)SFSaveDisk = -vBlock.volumeParam.ioVRefNum;
  564.         if (*(short*)FSFCBLen > 0) *(long*)CurDirStore = fsRtDirID;
  565.     };
  566. }
  567.  
  568. /*______________________________________________________________________
  569.  
  570.     utl_FlashButton - Flash Dialog Button.
  571.     
  572.     Entry:    theDialog = pointer to dialog.
  573.                 itemNo = item number of button to flash.
  574.                 
  575.     The push button is inverted for 8 ticks.  See HIN #10.
  576. _____________________________________________________________________*/
  577.  
  578.  
  579. void utl_FlashButton (DialogPtr theDialog, short itemNo)
  580.  
  581. {
  582.     short            itemType;            /* item type */
  583.     Handle        item;                    /* item handle */
  584.     Rect            box;                    /* item rectangle */
  585.     short            roundFactor;        /* rounded corner factor for InvertRoundRect */
  586.     long            tickEnd;                /* tick count to end flash */
  587.     
  588.     GetDItem(theDialog, itemNo, &itemType, &item, &box);
  589.     SetPort(theDialog);
  590.     roundFactor = (box.bottom - box.top)>>1;
  591.     InvertRoundRect(&box, roundFactor, roundFactor);
  592.     tickEnd = TickCount() + 8;
  593.     while (TickCount() < tickEnd);
  594.     InvertRoundRect(&box, roundFactor, roundFactor);
  595. }
  596.  
  597. /*______________________________________________________________________
  598.  
  599.     utl_FrameItem
  600.     
  601.     Entry:    theWindow = pointer to dialog window.
  602.                 itemNo = dialog item number.
  603.     
  604.     Exit:        dialog item rectangle framed.
  605.                 
  606.     This function is for use as a Dialog Manager user item procedure.
  607.     It is particularly useful for drawing rules (straight lines).  Simply
  608.     define the user item rectangle with bottom=top+1 or right=left+1.
  609. _____________________________________________________________________*/
  610.  
  611.  
  612. pascal void utl_FrameItem (WindowPtr theWindow, short itemNo)
  613.  
  614. {
  615.     short            itemType;            /* item type */
  616.     Handle        item;                    /* item handle */
  617.     Rect            box;                    /* item rectangle */
  618.  
  619.     GetDItem(theWindow, itemNo, &itemType, &item, &box);
  620.     FrameRect(&box);
  621. }
  622.  
  623. /*______________________________________________________________________
  624.  
  625.     utl_GetApplVol - Get the volume reference number of the application volume.
  626.     
  627.     Exit:        function result = volume reference number of the volume 
  628.                     containing the current application.
  629. _____________________________________________________________________*/
  630.  
  631.  
  632. short utl_GetApplVol (void)
  633.  
  634. {
  635.     short            vRefNum;                /* vol ref num */
  636.  
  637.     GetVRefNum(*(short*)(CurMap), &vRefNum);
  638.     return vRefNum;
  639. }
  640.  
  641. /*______________________________________________________________________
  642.  
  643.     utl_GetBlessedWDRefNum - Get the working directory reference number of
  644.         the Blessed Folder.
  645.     
  646.     
  647.     Exit:        function result = wdRefNum of blessed folder
  648. _____________________________________________________________________*/
  649.  
  650.  
  651. short utl_GetBlessedWDRefNum (void)
  652.  
  653. {
  654.     if (!GotSysEnviron) GetSysEnvirons();
  655.     return TheWorld.sysVRefNum;
  656. }
  657.  
  658. /*______________________________________________________________________
  659.  
  660.     utl_GetFontNumber - Get Font Number.
  661.     
  662.     Entry:    fontName = font name.
  663.     
  664.     Exit:        function result = true if font exists.
  665.                 fontNum = font number.
  666.                 
  667.     Copied from TN 191.
  668. _____________________________________________________________________*/
  669.  
  670.  
  671. Boolean utl_GetFontNumber (Str255 fontName, short *fontNum)
  672.  
  673. {
  674.     Str255        systemFontName;
  675.     
  676.     GetFNum(fontName, fontNum);
  677.     if (*fontNum) {
  678.         return true;
  679.     } else {
  680.         GetFontName(0, systemFontName);
  681.         return EqualString(fontName, systemFontName, false, false);
  682.     };
  683. }
  684.  
  685. /*______________________________________________________________________
  686.  
  687.     utl_GetLongSleep - Get Long Sleep Time.
  688.     
  689.     Exit:            function result = long sleep time.
  690.     
  691.     Returns the largest positive long integer (0x7fffffff) if the 
  692.     system version is > 0x04ff, else returns 50.  See TN 177.
  693. _____________________________________________________________________*/
  694.  
  695.  
  696. long utl_GetLongSleep (void)
  697.  
  698. {
  699.     if (!GotSysEnviron) GetSysEnvirons();
  700.     return (TheWorld.systemVersion > 0x04ff) ? 0x7fffffff : 50;
  701. }
  702.  
  703. /*______________________________________________________________________
  704.  
  705.     utl_GetMBarHeight - Get Menu Bar Height
  706.     
  707.     Exit:        function result = menu bar height.
  708.     
  709.     See TN 117.
  710. _____________________________________________________________________*/
  711.  
  712.  
  713. short utl_GetMBarHeight (void)
  714.  
  715. {
  716.     static short        mBHeight = 0;
  717.     
  718.     if (!mBHeight) {
  719.         mBHeight = utl_Rom64() ? 20 : *(short*)MBarHeight;
  720.     };
  721.     return mBHeight;
  722. }
  723.  
  724. /*______________________________________________________________________
  725.  
  726.     utl_GetNewControl - Get New Control.
  727.     
  728.     Entry:    controlID = resource id of CNTL resource.
  729.                 theWindow = pointer to window record.
  730.                 
  731.     Exit:        function result = handle to new control record.
  732.                 
  733.     This routine is identical to the Control Manager routine GetNewControl,
  734.     except it does not release or make purgeable the CNTL resource.
  735. _____________________________________________________________________*/
  736.  
  737.  
  738. ControlHandle utl_GetNewControl (short controlID, 
  739.     WindowPtr theWindow)
  740.     
  741. {
  742.     Rect            boundsRect;            /* boundary rectangle */
  743.     short            value;                /* initial control value */
  744.     Boolean        visible;                /* true if visible */
  745.     short            max;                    /* max control value */
  746.     short            min;                    /* min control value */
  747.     short            procID;                /* window proc id */
  748.     long            refCon;                /* refCon field for window record */
  749.     Str255        title;                /* window title */
  750.     Handle        theRez;                /* handle to CNTL resource */
  751.     
  752.     theRez = GetResource('CNTL', controlID);
  753.     boundsRect = *(Rect*)(*theRez);
  754.     value = *(short*)(*theRez+8);
  755.     visible = *(Boolean*)(*theRez+10);
  756.     max = *(short*)(*theRez+12);
  757.     min = *(short*)(*theRez+14);
  758.     procID = *(short*)(*theRez+16);
  759.     refCon = *(long*)(*theRez+18);
  760.     utl_CopyPString(title, *theRez+22);
  761.     return NewControl(theWindow, &boundsRect, title, visible, value,
  762.         min, max, procID, refCon);
  763. }
  764.  
  765. /*______________________________________________________________________
  766.  
  767.     utl_GetNewDialog - Get New Dialog.
  768.     
  769.     Entry:    dialogID = resource id of DLOG resource.
  770.                 dStorage = pointer to dialog record.
  771.                 behind = window to insert in back of.
  772.                 
  773.     Exit:        function result = pointer to new dialog record.
  774.                 
  775.     This routine is identical to the Dialog Manager routine GetNewDialog,
  776.     except it does not release or make purgeable the DLOG resource.
  777. _____________________________________________________________________*/
  778.  
  779.  
  780. DialogPtr utl_GetNewDialog (short dialogID, Ptr dStorage, 
  781.     WindowPtr behind)
  782.     
  783. {
  784.     Rect            boundsRect;            /* boundary rectangle */
  785.     short            procID;                /* window proc id */
  786.     Boolean        visible;                /* true if visible */
  787.     Boolean        goAwayFlag;            /* true if window has go away box */
  788.     long            refCon;                /* refCon field for window record */
  789.     Str255        title;                /* window title */
  790.     Handle        theRez;                /* handle to DLOG resource */
  791.     short            itemID;                /* rsrc id of item list */
  792.     Handle        items;                /* handle to item list */
  793.     
  794.     theRez = GetResource('DLOG', dialogID);
  795.     boundsRect = *(Rect*)(*theRez);
  796.     procID = *(short*)(*theRez+8);
  797.     visible = *(Boolean*)(*theRez+10);
  798.     goAwayFlag = *(Boolean*)(*theRez+12);
  799.     refCon = *(long*)(*theRez+14);
  800.     itemID = *(short*)(*theRez+18);
  801.     utl_CopyPString(title, *theRez+20);
  802.     items = GetResource('DITL', itemID);
  803.     return NewDialog(dStorage, &boundsRect, title, visible, procID, behind,
  804.         goAwayFlag, refCon, items);
  805. }
  806.  
  807. /*______________________________________________________________________
  808.  
  809.     utl_GetNewWindow - Get New Window.
  810.     
  811.     Entry:    windowID = resource id of WIND resource.
  812.                 wStorage = pointer to window record.
  813.                 behind = window to insert in back of.
  814.                 
  815.     Exit:        function result = pointer to new window record.
  816.                 
  817.     This routine is identical to the Window Manager routine GetNewWindow,
  818.     except it does not release or make purgeable the WIND resource.
  819. _____________________________________________________________________*/
  820.  
  821.  
  822. WindowPtr utl_GetNewWindow (short windowID, Ptr wStorage, 
  823.     WindowPtr behind)
  824.     
  825. {
  826.     Rect            boundsRect;            /* boundary rectangle */
  827.     short            procID;                /* window proc id */
  828.     Boolean        visible;                /* true if visible */
  829.     Boolean        goAwayFlag;            /* true if window has go away box */
  830.     long            refCon;                /* refCon field for window record */
  831.     Str255        title;                /* window title */
  832.     Handle        theRez;                /* handle to WIND resource */
  833.     
  834.     theRez = GetResource('WIND', windowID);
  835.     boundsRect = *(Rect*)(*theRez);
  836.     procID = *(short*)(*theRez+8);
  837.     visible = *(Boolean*)(*theRez+10);
  838.     goAwayFlag = *(Boolean*)(*theRez+12);
  839.     refCon = *(long*)(*theRez+14);
  840.     utl_CopyPString(title, *theRez+18);
  841.     return NewWindow(wStorage, &boundsRect, title, visible, procID, behind,
  842.         goAwayFlag, refCon);
  843. }
  844.  
  845. /*______________________________________________________________________
  846.  
  847.     utl_GetSysVol - Get the volume reference number of the system volume.
  848.     
  849.     Exit:        function result = volume reference number of the volume 
  850.                     containing the currently active system file.
  851. _____________________________________________________________________*/
  852.  
  853.  
  854. short utl_GetSysVol (void)
  855.  
  856. {
  857.     
  858.     short            vRefNum;                /* vol ref num */
  859.  
  860.     GetVRefNum(*(short*)(SysMap), &vRefNum);
  861.     return vRefNum;
  862. }
  863.  
  864. /*______________________________________________________________________
  865.  
  866.     utl_GetSysWD - Get WDRefNum of Blessed Folder.
  867.     
  868.     Exit:        function result = WDRefNum of Blessed Folder.
  869. _____________________________________________________________________*/
  870.  
  871.  
  872. short utl_GetSysWD (void)
  873.  
  874. {
  875.     if (!GotSysEnviron) GetSysEnvirons();
  876.     return TheWorld.sysVRefNum;
  877. }
  878.  
  879. /*______________________________________________________________________
  880.  
  881.     utl_GetWindGD - Get the GDevice containing a window.
  882.     
  883.     Entry:    theWindow = pointer to window.
  884.     
  885.     Exit:        gd = handle to GDevice, or nil if no color QD.
  886.                 screenRect = bounding rectangle of GDevice, or 
  887.                     qd.screenBits.bounds if no color QD.
  888.                 windRect = content rectangle of window, in global
  889.                     coords.
  890.                 hasMB = true if this screen contains the menu bar.
  891.                 
  892.     The routine determines the GDevice (screen) containing the maximum
  893.     intersection with a window.  See TN 79.
  894. _____________________________________________________________________*/
  895.  
  896.  
  897. void utl_GetWindGD (WindowPtr theWindow, GDHandle *gd, 
  898.     Rect *screenRect, Rect *windRect, Boolean *hasMB)
  899.     
  900. {
  901.     GrafPtr            savePort;            /* saved grafport */
  902.     Rect                sectRect;            /* intersection rect */
  903.     GDHandle            curDevice;            /* current GDevice */
  904.     GDHandle            dominantDevice;    /* dominant GDevice */
  905.     long                sectArea;            /* intersection area */
  906.     long                maxArea;                /* max intersection area */
  907.     
  908.     *windRect = theWindow->portRect;
  909.     GetPort(&savePort);
  910.     SetPort(theWindow);
  911.     LocalToGlobal((Point*)&windRect->top);
  912.     LocalToGlobal((Point*)&windRect->bottom);
  913.     if (utl_HaveColor()) {
  914.         windRect->top -= titleBarHeight;
  915.         curDevice = GetDeviceList();
  916.         maxArea = 0;
  917.         dominantDevice = nil;
  918.         while (curDevice) {
  919.             if (TestDeviceAttribute(curDevice, screenDevice) &&
  920.                 TestDeviceAttribute(curDevice, screenActive)) {
  921.                 SectRect(windRect, &(**curDevice).gdRect, §Rect);
  922.                 sectArea = (long)(sectRect.right - sectRect.left) * 
  923.                     (long)(sectRect.bottom - sectRect.top);
  924.                 if (sectArea > maxArea) {
  925.                     maxArea = sectArea;
  926.                     dominantDevice = curDevice;
  927.                 };
  928.             };
  929.             curDevice = GetNextDevice(curDevice);
  930.         };
  931.         windRect->top += titleBarHeight;
  932.         if (dominantDevice) {
  933.             *gd = dominantDevice;
  934.             *screenRect = (**dominantDevice).gdRect;
  935.             *hasMB = dominantDevice == GetMainDevice();
  936.         } else {
  937.             *gd = nil;
  938.             *screenRect = qd.screenBits.bounds;
  939.             *hasMB = true;
  940.         };    
  941.     } else {
  942.         *gd = nil;
  943.         *screenRect = qd.screenBits.bounds;
  944.         *hasMB = true;
  945.     };
  946.     SetPort(savePort);
  947. }
  948.  
  949. /*______________________________________________________________________
  950.  
  951.     utl_GetVolFilCnt - Get the number of files on a volume.
  952.     
  953.     Entry:    volRefNum = volume reference number of volume.
  954.     
  955.     Exit:        function result = number of files on volume.
  956.     
  957.     For serdver volumes this function always returns 0.
  958. _____________________________________________________________________*/
  959.  
  960.  
  961. long utl_GetVolFilCnt (short volRefNum)
  962.  
  963. {
  964.     HParamBlockRec        pBlock;        /* param block for PHBGetVInfo */
  965.     
  966.     pBlock.volumeParam.ioNamePtr = nil;
  967.     pBlock.volumeParam.ioVRefNum = volRefNum;
  968.     pBlock.volumeParam.ioVolIndex = 0;
  969.     (void) PBHGetVInfo(&pBlock, false);
  970.     return pBlock.volumeParam.ioVFilCnt;
  971. }
  972.  
  973. /*______________________________________________________________________
  974.  
  975.     utl_HaveColor - Determine if system has color QuickDraw.
  976.  
  977.     Exit:        function result = true if we have color QD.
  978. _____________________________________________________________________*/
  979.  
  980.  
  981. Boolean utl_HaveColor (void)
  982.  
  983. {
  984.     if (!GotSysEnviron) GetSysEnvirons();
  985.     return TheWorld.hasColorQD;
  986. }
  987.  
  988. /*______________________________________________________________________
  989.  
  990.     utl_HaveSound - Determine if system has the Sound Manager.
  991.  
  992.     Exit:        function result = true if we have sound.
  993. _____________________________________________________________________*/
  994.  
  995.  
  996. Boolean utl_HaveSound (void)
  997.  
  998. {
  999.     if (!GotSysEnviron) GetSysEnvirons();
  1000.     return (TheWorld.systemVersion >= 0x0602);
  1001. }
  1002.  
  1003. /*______________________________________________________________________
  1004.  
  1005.     utl_InitSpinCursor - Initialize animated cursor.
  1006.     
  1007.     Entry:    cursArray = array of handles to cursors.
  1008.                 numCurs = number of cursors to rotate.
  1009.                 tickInterval = interval between cursor rotations.
  1010. _____________________________________________________________________*/
  1011.  
  1012.  
  1013. void utl_InitSpinCursor (CursHandle *cursArray, short numCurs, 
  1014.     short tickInterval)
  1015.  
  1016. {
  1017.     CursHandle        h;
  1018.  
  1019.     CursArray = cursArray;
  1020.     CurCurs = 0;
  1021.     NumCurs = numCurs;
  1022.     TickInterval = tickInterval;
  1023.     LastTick = TickCount();
  1024.     h = *cursArray;
  1025.     SetCursor(*h);
  1026. }
  1027.  
  1028. /*______________________________________________________________________
  1029.  
  1030.     utl_InvalGrow - Invalidate Grow Icon.
  1031.     
  1032.     Entry:            theWindow = pointer to window.
  1033.     
  1034.     This routine should be called before and after calling SizeWindow
  1035.     for windows with grow icons.
  1036. _____________________________________________________________________*/
  1037.  
  1038.  
  1039. void utl_InvalGrow (WindowPtr theWindow)
  1040.  
  1041. {
  1042.     Rect            r;            /* rect to be invalidated */
  1043.     
  1044.     r = theWindow->portRect;
  1045.     r.top = r.bottom - 15;
  1046.     r.left = r.right - 15;
  1047.     InvalRect(&r);
  1048. };
  1049.  
  1050. /*______________________________________________________________________
  1051.  
  1052.     utl_IsDAWindow - Check to see if a window is a DA.
  1053.     
  1054.     Entry:    theWindow = pointer to dialog window.
  1055.                 
  1056.     
  1057.     Exit:        function result = true if DA window.
  1058. _____________________________________________________________________*/
  1059.  
  1060.  
  1061. Boolean utl_IsDAWindow (WindowPtr theWindow)
  1062.  
  1063. {
  1064.     return ((WindowPeek)theWindow)->windowKind < 0;
  1065. }
  1066.  
  1067. /*______________________________________________________________________
  1068.  
  1069.     utl_IsLaser - Check Printer for LaserWriter.
  1070.     
  1071.     Entry:    hPrint = handle to print record.
  1072.     
  1073.     Exit:        function result = true if LaserWriter.
  1074.     
  1075. _____________________________________________________________________*/
  1076.  
  1077.  
  1078. Boolean utl_IsLaser (THPrint hPrint)
  1079.  
  1080. {
  1081.     unsigned char    wDev;                /* printer device */
  1082.  
  1083.     wDev = (**hPrint).prStl.wDev >> 8;
  1084.     return wDev==3 || wDev==4;
  1085. }
  1086.  
  1087. /*______________________________________________________________________
  1088.  
  1089.     utl_LockControls - Lock Window Controls
  1090.     
  1091.     Entry:    theWindow = pointer to window.
  1092.     
  1093.     This routine moves all the control records in a window high and
  1094.     locks them.  It should be called immediately after creating a new
  1095.     window, and before drawing it.  It works around errors in the Control
  1096.     Manager which showed up when I was testing with TMON's heap scramble and
  1097.     purge option.
  1098. _____________________________________________________________________*/
  1099.  
  1100.  
  1101. void utl_LockControls (WindowPtr theWindow)
  1102.  
  1103. {
  1104.     ControlHandle        theControl;        /* handle to control */
  1105.  
  1106.     theControl = ((WindowPeek)theWindow)->controlList;
  1107.     while (theControl) {
  1108.         MoveHHi((Handle)theControl);
  1109.         HLock((Handle)theControl);
  1110.         theControl = (**theControl).nextControl;
  1111.     };
  1112. }
  1113.  
  1114. /*______________________________________________________________________
  1115.  
  1116.     utl_PlotSmallIcon - Draw a small icon.
  1117.     
  1118.     Entry:    theRect = rectangle in which to draw the small icon.
  1119.                 theHandle = handle to small icon.
  1120.                     
  1121.     For best results, the rectangle should be exactly 16 pixels square.
  1122. _____________________________________________________________________*/
  1123.  
  1124.  
  1125. void utl_PlotSmallIcon (Rect *theRect, Handle theHandle)
  1126.  
  1127. {
  1128.     BitMap        srcBits;            /* Source bitmap for CopyBits */
  1129.     
  1130.     MoveHHi(theHandle);
  1131.     HLock(theHandle);
  1132.     srcBits.baseAddr = *theHandle;
  1133.     srcBits.rowBytes = 2;
  1134.     SetRect(&srcBits.bounds, 0, 0, 16, 16);
  1135.     CopyBits(&srcBits, &qd.thePort->portBits, &srcBits.bounds, theRect,
  1136.         srcCopy, nil);
  1137.     HUnlock(theHandle);
  1138. }
  1139.  
  1140. /*______________________________________________________________________
  1141.  
  1142.     utl_PlugParams - Plug parameters into message.
  1143.     
  1144.     Entry:    line1 = input line.
  1145.                 p0, p1, p2, p3 = parameters.
  1146.                     
  1147.     Exit:        line2 = output line.
  1148.                     
  1149.     This routine works just like the toolbox routine ParamText.
  1150.     The input line may contain place-holders ^0, ^1, ^2, and ^3, 
  1151.     which are replaced by the parameters p0-p3.  The input line
  1152.     must not contain any other ^ characters.  The input and output lines
  1153.     may not be the same string.  Pass nil for parameters which don't
  1154.     occur in line1.  If the output line exceeds 255 characters it's
  1155.     truncated.
  1156. _____________________________________________________________________*/
  1157.  
  1158.  
  1159. void utl_PlugParams (Str255 line1, Str255 line2, Str255 p0, 
  1160.     Str255 p1, Str255 p2, Str255 p3)
  1161.  
  1162. {
  1163.     char                *in;            /* pointer to cur pos in input line */
  1164.     char                *out;            /* pointer to cur pos in output line */
  1165.     char                *inEnd;        /* pointer to end of input line */
  1166.     char                *outEnd;        /* pointer to end of output line */
  1167.     char                *param;        /* pointer to param to be plugged */
  1168.     short                len;            /* length of param */
  1169.     
  1170.     in = line1+1;
  1171.     out = line2+1;
  1172.     inEnd = line1 + 1 + *line1;
  1173.     outEnd = line2 + 256;
  1174.     while (in < inEnd ) {
  1175.         if (*in == '^') {
  1176.             in++;
  1177.             if (in >= inEnd) break;
  1178.             switch (*in++) {
  1179.                 case '0':
  1180.                     param = p0;
  1181.                     break;
  1182.                 case '1':
  1183.                     param = p1;
  1184.                     break;
  1185.                 case '2':
  1186.                     param = p2;
  1187.                     break;
  1188.                 case '3':
  1189.                     param = p3;
  1190.                     break;
  1191.                 default:
  1192.                     continue;
  1193.             };
  1194.             if (!param) continue;
  1195.             len = *param;
  1196.             if (out + len > outEnd) len = outEnd - out;
  1197.             memcpy(out, param+1, len);
  1198.             out += len;
  1199.         } else {
  1200.             if (out >= outEnd) break;
  1201.             *out++ = *in++;
  1202.         };
  1203.     };
  1204.     *line2 = out - (line2+1);
  1205. };
  1206.  
  1207. /*______________________________________________________________________
  1208.  
  1209.     utl_RestoreWindowPos - Restore Window Position.
  1210.     
  1211.     Entry:    theWindow = pointer to window.
  1212.                 userState = saved user state rectangle for the window.
  1213.                 zoomed = true if window in zoomed state when saved.
  1214.                 offset = pixel offset used in DragRect calls.
  1215.                 computeStdState = pointer to function to compute standard state.
  1216.                 computeDefState = pointer to function to compute default state.
  1217.     
  1218.     Exit:        window position, size, and zoom state restored.
  1219.                 userState = new user state.
  1220.                 
  1221.     See HIN 6: "When reopening a movable window, check its saved 
  1222.     position.  If the window is in a position to which the user could
  1223.     have dragged it, then leave it there.  If the window can be zoomed
  1224.     and was in the zoomed state when it was last closed, put it in the
  1225.     zoomed state again.  (Note that the current and previous zoomed states
  1226.     are not necessarily the same, since the window may be reopened on a
  1227.     different monitor.)  If the window is not in a position to which the
  1228.     user could have dragged it, then it must be relocated, so use the
  1229.     default location.  However, do not automatically use the default size
  1230.     when using the default location; if the entire window would be visible
  1231.     using the default location and stored size, then use the stored size."
  1232.     
  1233.     The "offset" parameter is usually 4.  When initializing the boundary rectangle
  1234.     for DragWindow calls, normally the boundary rectangle of the desktop gray
  1235.     region is inset by 4 pixels.  If some value other than 4 is used, it should
  1236.     be passed to RestoreWindowPos as the "offset" parameter.
  1237.     
  1238.     The computeStdState function is passed a pointer to the window.  Given
  1239.     the userState in the window zoom info, it must compute the standard
  1240.     (zoom) state in the window zoom info.  This is an application-dependent
  1241.     function.
  1242.     
  1243.     The computeDefState function must determine the default position and
  1244.     size of a new window, and return the result as a rectangle.  This may
  1245.     involve invocation of a staggering algorithm or some other algorithm.
  1246.     This is an application-dependent function.
  1247. _____________________________________________________________________*/
  1248.  
  1249.  
  1250. void utl_RestoreWindowPos (WindowPtr theWindow, Rect *userState, 
  1251.     Boolean zoomed, short offset,
  1252.     utl_ComputeStdStatePtr computeStdState,
  1253.     utl_ComputeDefStatePtr computeDefState)
  1254.  
  1255. {
  1256.     WindowPeek        w;                    /* window pointer */
  1257.     short                userHeight;        /* height of userState */
  1258.     short                userWidth;        /* width of userState */
  1259.     Rect                r;                    /* scratch rectangle */
  1260.     RgnHandle        rgn;                /* scratch region */
  1261.     Rect                stdState;        /* standard state */
  1262.     short                windHeight;        /* window height */
  1263.     short                windWidth;        /* window width */
  1264.  
  1265.     w = (WindowPeek)theWindow;
  1266.     if (!utl_CouldDrag(userState, offset)) {
  1267.         userHeight = userState->bottom - userState->top;
  1268.         userWidth = userState->right - userState->left;
  1269.         (*computeDefState)(theWindow, userState);
  1270.         if (!zoomed) {
  1271.             r = *userState;
  1272.             r.bottom = r.top + userHeight;
  1273.             r.right = r.left + userWidth;
  1274.             r.top -= titleBarHeight;
  1275.             InsetRect(&r, -1, -1);
  1276.             rgn = NewRgn();
  1277.             RectRgn(rgn, &r);
  1278.             DiffRgn(rgn, *(RgnHandle*)GrayRgn, rgn);
  1279.             if (EmptyRgn(rgn)) {
  1280.                 userState->bottom = userState->top + userHeight;
  1281.                 userState->right = userState->left + userWidth;
  1282.             };
  1283.             DisposeRgn(rgn);
  1284.         };
  1285.     };
  1286.     MoveWindow(theWindow, userState->left, userState->top, false);
  1287.     windHeight = userState->bottom - userState->top;
  1288.     windWidth = userState->right - userState->left;
  1289.     SizeWindow(theWindow, windWidth, windHeight, true);
  1290.     if (w->dataHandle && w->spareFlag) {
  1291.         (**((WStateData**)w->dataHandle)).userState = *userState;
  1292.         (*computeStdState)(theWindow);
  1293.         if (zoomed) {
  1294.             stdState = (**((WStateData**)w->dataHandle)).stdState;
  1295.             MoveWindow(theWindow, stdState.left, stdState.top, false);
  1296.             windHeight = stdState.bottom - stdState.top;
  1297.             windWidth = stdState.right - stdState.left;
  1298.             SizeWindow(theWindow, windWidth, windHeight, true);
  1299.         };
  1300.     };
  1301. }
  1302.  
  1303. /*______________________________________________________________________
  1304.  
  1305.     utl_Rom64 - Check to see if we have the old 64K ROM.
  1306.     
  1307.     Exit:        function result = true if 64K ROM.
  1308. _____________________________________________________________________*/
  1309.  
  1310.  
  1311. Boolean utl_Rom64 (void)
  1312.  
  1313. {
  1314.     return *(short*)ROM85 < 0;
  1315. }
  1316.  
  1317. /*______________________________________________________________________
  1318.  
  1319.     utl_RFSanity - Check a Resource File's Sanity.
  1320.     
  1321.     Entry:    *fName = file name.
  1322.     
  1323.     Exit:        *sane = true if resource fork is sane.
  1324.                 function result = error code.
  1325.                 
  1326.     This routine checks the sanity of a resource file.  The Resource Manager
  1327.     does not do error checking, and can bomb or hang if you use it to open
  1328.     a damaged resource file.  This routine can be called first to precheck
  1329.     the file.
  1330.     
  1331.     The routine checks the entire resource map.
  1332.     
  1333.     It is the caller's responsibility to set the proper default volume and
  1334.     directory.
  1335. _____________________________________________________________________*/
  1336.  
  1337.  
  1338. OSErr utl_RFSanity (Str255 fName, Boolean *sane)
  1339.  
  1340. {
  1341.     ParamBlockRec        fBlock;            /* file param block */
  1342.     short                    refNum;            /* file refnum */
  1343.     long                    count;            /* number of bytes to read */
  1344.     long                    logEOF;            /* logical EOF */
  1345.     Ptr                    map;                /* pointer to resource map */
  1346.     unsigned long        dataLWA;            /* offset in file of data end */
  1347.     unsigned long        mapLWA;            /* offset in file of map end */
  1348.     unsigned short        typeFWA;            /* offset from map begin to type list */
  1349.     unsigned short        nameFWA;            /* offset from map begin to name list */
  1350.     unsigned char        *pType;            /* pointer into type list */
  1351.     unsigned char        *pName;            /* pointer to start of name list */
  1352.     unsigned char        *pMapEnd;        /* pointer to end of map */
  1353.     short                    nType;            /* number of resource types in map */
  1354.     unsigned char        *pTypeEnd;        /* pointer to end of type list */
  1355.     short                    nRes;                /* number of resources of given type */
  1356.     unsigned short        refFWA;            /* offset from type list to ref list */
  1357.     unsigned char        *pRef;            /* pointer into reference list */
  1358.     unsigned char        *pRefEnd;        /* pointer to end of reference list */
  1359.     unsigned short        resNameFWA;        /* offset from name list to resource name */
  1360.     unsigned char        *pResName;        /* pointer to resource name */
  1361.     unsigned long        resDataFWA;        /* offset from data begin to resource data */
  1362.     Boolean                mapOK;            /* true if map is sane */
  1363.     OSErr                    rCode;            /* error code */
  1364.     
  1365.     struct {
  1366.         unsigned long        dataFWA;        /* offset in file of data */
  1367.         unsigned long        mapFWA;        /* offset in file of map */
  1368.         unsigned long        dataLen;        /* data area length */
  1369.         unsigned long        mapLen;        /* map area length */
  1370.     } header;
  1371.     
  1372.     /* Open the resource file. */
  1373.     
  1374.     fBlock.ioParam.ioNamePtr = fName;
  1375.     fBlock.ioParam.ioVRefNum = 0;
  1376.     fBlock.ioParam.ioVersNum = 0;
  1377.     fBlock.ioParam.ioPermssn = fsRdPerm;
  1378.     fBlock.ioParam.ioMisc = nil;
  1379.     if (rCode = PBOpenRF(&fBlock, false)) {
  1380.         if (rCode == fnfErr) {
  1381.             *sane = true;
  1382.             return noErr;
  1383.         } else {
  1384.             return rCode;
  1385.         };
  1386.     };
  1387.     
  1388.     /* Get the logical eof of the file. */
  1389.     
  1390.     refNum = fBlock.ioParam.ioRefNum;
  1391.     if (rCode = GetEOF(refNum, &logEOF)) return rCode;
  1392.     if (!logEOF) {
  1393.         *sane = true;
  1394.         if (rCode = FSClose(refNum)) return rCode;
  1395.         return noErr;
  1396.     };
  1397.     
  1398.     /* Read and validate the resource header. */
  1399.     
  1400.     count = 16;
  1401.     if (rCode = FSRead(refNum, &count, (Ptr)&header)) {
  1402.         FSClose(refNum);
  1403.         return rCode;
  1404.     };
  1405.     dataLWA = header.dataFWA + header.dataLen;
  1406.     mapLWA = header.mapFWA + header.mapLen;
  1407.     mapOK = count == 16 && header.mapLen > 28 &&
  1408.         header.dataFWA < 0x01000000 && header.mapFWA < 0x01000000 &&
  1409.         dataLWA <= logEOF && mapLWA <= logEOF &&
  1410.         (dataLWA <= header.mapFWA || mapLWA <= header.dataFWA);
  1411.         
  1412.     /* Read the resource map. */
  1413.     
  1414.     map = nil;
  1415.     if (mapOK) {
  1416.         map = NewPtr(header.mapLen);
  1417.         if (!(rCode = SetFPos(refNum, fsFromStart, header.mapFWA))) {
  1418.             count = header.mapLen;
  1419.             rCode = FSRead(refNum, &count, map);
  1420.         };
  1421.     };
  1422.     
  1423.     /* Verify the type list and name list offsets. */
  1424.     
  1425.     if (!rCode) {
  1426.         typeFWA = *(unsigned short*)(map+24);
  1427.         nameFWA = *(unsigned short*)(map+26);
  1428.         mapOK = typeFWA == 28 && nameFWA >= typeFWA && nameFWA <= header.mapLen &&
  1429.             !(typeFWA & 1) && !(nameFWA & 1);
  1430.     };
  1431.     
  1432.     /* Verify the type list, reference lists, and name list. */
  1433.     
  1434.     if (mapOK) {
  1435.         pType = map + typeFWA;
  1436.         pName = map + nameFWA;
  1437.         pMapEnd = map + header.mapLen;
  1438.         nType = *(short*)pType + 1;
  1439.         pType += 2;
  1440.         pTypeEnd = pType + (nType<<3);
  1441.         if (mapOK = pTypeEnd <= pMapEnd) {
  1442.             while (pType < pTypeEnd) {
  1443.                 nRes = *(short*)(pType+4) + 1;
  1444.                 refFWA = *(unsigned short*)(pType+6);
  1445.                 pRef = map + typeFWA + refFWA;
  1446.                 pRefEnd = pRef + 12*nRes;
  1447.                 if (!(mapOK = pRef >= pTypeEnd && pRef < pName && 
  1448.                     !(refFWA & 1))) break;
  1449.                 while (pRef < pRefEnd) {
  1450.                     resNameFWA = *(unsigned short*)(pRef+2);
  1451.                     if (resNameFWA != 0xFFFF) {
  1452.                         pResName = pName + resNameFWA;
  1453.                         if (!(mapOK = pResName + *pResName < pMapEnd)) break;
  1454.                     };
  1455.                     resDataFWA = *(unsigned long*)(pRef+4) & 0x00FFFFFF;
  1456.                     if (!(mapOK = header.dataFWA + resDataFWA < dataLWA)) break;
  1457.                     pRef += 12;
  1458.                 };
  1459.                 if (!mapOK) break;
  1460.                 pType += 8;
  1461.             };
  1462.         };
  1463.     };
  1464.     
  1465.     /* Dispose of the resource map, close the file and return. */
  1466.     
  1467.     if (map) DisposPtr(map);
  1468.     if (!rCode) {
  1469.         rCode = FSClose(refNum);
  1470.     } else {
  1471.         (void) FSClose(refNum);
  1472.     };
  1473.     *sane = mapOK;
  1474.     return rCode;
  1475. }
  1476.  
  1477. /*______________________________________________________________________
  1478.  
  1479.     utl_SaveWindowPos - Save Window Position.
  1480.     
  1481.     Entry:    theWindow = window pointer.
  1482.     
  1483.     Exit:        userState = user state rectangle of the window.
  1484.                 zoomed = true if window zoomed.
  1485.                 
  1486.     See HIN 6: "Before closing a movable window, check to see if its
  1487.     location or size have changed.  If so, save the new location and
  1488.     size.  If the window can be zoomed, save the user state and also 
  1489.     save whether or not the window is in the zoomed (standard) sate."
  1490.     
  1491.     We assume in this routine that the caller has already kept track
  1492.     of the fact that this window's location or size has changed, and that
  1493.     we do indeed need to save the location and size.
  1494.     
  1495.     This routine only works if the window's origin has not been offset.
  1496. _____________________________________________________________________*/
  1497.  
  1498.  
  1499. void utl_SaveWindowPos (WindowPtr theWindow, Rect *userState, Boolean *zoomed)
  1500.  
  1501. {
  1502.     GrafPtr        savedPort;        /* saved grafport */
  1503.     WindowPeek    w;                    /* window pointer */
  1504.     Rect            curState;        /* current window rect, global coords */
  1505.     Rect            stdState;        /* standard (zoom) rect, global coords */
  1506.     Point            p;                    /* scratch point */
  1507.     Rect            r;                    /* scratch rect */
  1508.     
  1509.     GetPort(&savedPort);
  1510.     SetPort(theWindow);
  1511.     SetPt(&p, 0, 0);
  1512.     LocalToGlobal(&p);
  1513.     curState = theWindow->portRect;
  1514.     OffsetRect(&curState, p.h, p.v);
  1515.     w = (WindowPeek)theWindow;
  1516.     if (w->dataHandle && w->spareFlag) {
  1517.         /* This window supports zooming */
  1518.         /* Determine if window is zoomed.  The criteria is that both the
  1519.             top left and bottom right corners of the current window rectangle
  1520.             and the standard (zoom) rectangle must be within 7 pixels of each
  1521.             other.  This is the same algorithm as the one used by the standard
  1522.             system window definition function. */
  1523.         *zoomed = false;
  1524.         stdState = (**((WStateData**)w->dataHandle)).stdState;
  1525.         SetPt(&p, curState.left, curState.top);
  1526.         SetRect(&r, stdState.left, stdState.top, stdState.left, stdState.top);
  1527.         InsetRect(&r, -7, -7);
  1528.         if (PtInRect(p, &r)) {
  1529.             SetPt(&p, curState.right, curState.bottom);
  1530.             SetRect(&r, stdState.right, stdState.bottom, stdState.right,
  1531.                 stdState.bottom);
  1532.             InsetRect(&r, -7, -7);
  1533.             *zoomed = PtInRect(p, &r);
  1534.         };
  1535.         if (*zoomed) {
  1536.             *userState = (**((WStateData**)w->dataHandle)).userState;
  1537.         } else {
  1538.             *userState = curState;
  1539.         };
  1540.     } else {
  1541.         /* This window does not support zooming. */
  1542.         *zoomed = false;
  1543.         *userState = curState;
  1544.     };
  1545.     SetPort(savedPort);
  1546. }
  1547.  
  1548. /*______________________________________________________________________
  1549.  
  1550.     utl_ScaleFontSize - Scale Font Size
  1551.     
  1552.     Entry:    fontNum = font number.
  1553.                 fontSize = nominal font size.
  1554.                 percent = percent change in size.
  1555.                 laser = true if laserwriter.
  1556.     
  1557.     Exit:        function result = scaled font size.
  1558.                 
  1559.     The nominal font size is multiplied by the percentage,
  1560.     then truncated.  For non-laserwriters it is then rounded down
  1561.     to the nearest font size which is available in that true size,
  1562.     without font manager scaling, or which can be generated by doubling
  1563.     an existing font size.
  1564. _____________________________________________________________________*/
  1565.  
  1566.  
  1567. short utl_ScaleFontSize (short fontNum, short fontSize, short percent,
  1568.     Boolean laser)
  1569.     
  1570. {
  1571.     short            nSize;                /* new size */
  1572.     short            x;                        /* new test size */
  1573.     
  1574.     nSize = fontSize * percent / 100;
  1575.     if (!laser) {
  1576.         x = nSize;
  1577.         while (x > 0) {
  1578.             if (RealFont(fontNum, x)) break;
  1579.             if (!(x&1) && RealFont(fontNum, x>>1)) break;
  1580.             x--;
  1581.         };
  1582.         if (x) nSize = x;
  1583.     };
  1584.     return nSize;
  1585. }
  1586.  
  1587. /*______________________________________________________________________
  1588.  
  1589.     utl_SpinCursor - Animate cursor.
  1590.     
  1591.     After calling InitSpinCursor to initialize an animated cursor, call
  1592.     SpinCursor periodically to make the cursor animate.
  1593. _____________________________________________________________________*/
  1594.  
  1595.  
  1596. void utl_SpinCursor (void)
  1597.  
  1598. {
  1599.     CursHandle        h;                /* handle to cursor */
  1600.     long                ticksNow;    /* current tick count */
  1601.     
  1602.     ticksNow = TickCount();
  1603.     if (ticksNow < LastTick + TickInterval) return;
  1604.     LastTick = ticksNow;
  1605.     CurCurs++;
  1606.     if (CurCurs >= NumCurs) CurCurs = 0;
  1607.     h = CursArray[CurCurs];
  1608.     SetCursor(*h);
  1609. }
  1610.  
  1611. /*______________________________________________________________________
  1612.  
  1613.     utl_StaggerWindow - Stagger a New Window
  1614.     
  1615.     Entry:    windRect = window portrect.
  1616.                 initialOffset = intitial pixel offset of window from corner.
  1617.                 offset = offset for subsequent staggered windows.
  1618.     
  1619.     Exit:        pos = window position.
  1620.     
  1621.     According to HIN 6, a new window should be positioned as follows:
  1622.     "The first document window should be positioned in the upper-left corner
  1623.     of the gray area of the main screen (the screen with the menu bar).
  1624.     Each additional independent window should be staggered from the upper-left
  1625.     corner of the screen that contains the largest portion of the
  1626.     frontmost window."  Also, "When a window is used or closed, its original
  1627.     position becomes available again.  The next window opened should use this
  1628.     position.  Similarly, if a window is moved onto a previously available
  1629.     position, that position becomes unavailable again."
  1630.     
  1631.     This routine implements these rules.  A position is considered to be
  1632.     "unavailable" if some other window is within (offset+1)/2 pixels of the
  1633.     position in both the vertical and horizontal directions.
  1634.     
  1635.     If all slots are occupied, the routine attempts to locate the first one
  1636.     which is occupied by only one other window.  If this attempt fails, it
  1637.     tries to locate one which is occupied by only two other windows, etc.
  1638.     Thus, if the screen is filled with staggered windows, subsequent windows
  1639.     will be staggered on top of the existing ones, forming a second "layer."
  1640.     If this layer fills up, a third layer is started, etc.
  1641. _____________________________________________________________________*/
  1642.  
  1643.  
  1644. void utl_StaggerWindow (Rect *windRect, short initialOffset, short offset, 
  1645.     Point *pos)
  1646.  
  1647. {
  1648.     GrafPtr            savedPort;        /* saved grafport */
  1649.     short                offsetDiv2;        /* offset/2 */
  1650.     short                windHeight;        /* window height */
  1651.     short                windWidth;        /* window width */
  1652.     WindowPtr        frontWind;        /* pointer to front window */
  1653.     GDHandle            gd;                /* GDevice */
  1654.     Rect                screenRect;        /* screen rectangle */
  1655.     Rect                junkRect;        /* window rectangle */
  1656.     Boolean            hasMB;            /* true if screen has menu bar */
  1657.     Point                initPos;            /* initial staggered window position */
  1658.     Point                curPos;            /* current staggered window position */
  1659.     WindowPtr        curWind;            /* pointer to current window */
  1660.     Point                windPos;            /* current window position */
  1661.     short                deltaH;            /* horizontal distance */
  1662.     short                deltaV;            /* vertical distance */
  1663.     short                layer;            /* layer number */
  1664.     short                nOccupied;        /* number windows occupying cur pos */
  1665.     
  1666.     GetPort(&savedPort);
  1667.     offsetDiv2 = (offset+1)>>1;
  1668.     windHeight = windRect->bottom - windRect->top;
  1669.     windWidth = windRect->right - windRect->left;
  1670.     frontWind = FrontWindow();
  1671.     if (frontWind) {
  1672.         utl_GetWindGD(frontWind, &gd, &screenRect, &junkRect, &hasMB);
  1673.     } else {
  1674.         screenRect = qd.screenBits.bounds;
  1675.         hasMB = true;
  1676.     };
  1677.     if (hasMB) screenRect.top += utl_GetMBarHeight();
  1678.     SetPt(&initPos, screenRect.left + initialOffset + 1, 
  1679.         screenRect.top + initialOffset + titleBarHeight);
  1680.     layer = 1;
  1681.     while (true) {
  1682.         /* Test each layer number "layer", starting with 1 and incrementing by 1.
  1683.             Break out of the loop when we find a position curPos which
  1684.             is "occupied" by fewer than "layer" other windows. */
  1685.         curPos = initPos;
  1686.         while (true) {
  1687.             /* Test each possible position curPos.  Break out of the loop
  1688.                 when we have exhaused the possible positions, or when we
  1689.                 have located one which has < layer "occupants". */
  1690.             curWind = frontWind;
  1691.             if (curPos.v + windHeight >= screenRect.bottom ||
  1692.                 curPos.h + windWidth >= screenRect.right) {
  1693.                 break;
  1694.             };
  1695.             nOccupied = 0;
  1696.             while (curWind) {
  1697.                 /* Scan the window list and count up how many of them "occupy"
  1698.                     the current location curPos.  Break out of the loop when
  1699.                     we reach the end of the list, or when the count is >=
  1700.                     layer. */
  1701.                 SetPt(&windPos, 0, 0);
  1702.                 SetPort(curWind);
  1703.                 LocalToGlobal(&windPos);
  1704.                 deltaH = curPos.h - windPos.h;
  1705.                 deltaV = curPos.v - windPos.v;
  1706.                 if (deltaH < 0) deltaH = -deltaH;
  1707.                 if (deltaV < 0) deltaV = -deltaV;
  1708.                 if (deltaH <= offsetDiv2 && deltaV <= offsetDiv2) {
  1709.                     nOccupied++;
  1710.                     if (nOccupied >= layer) break;
  1711.                 };
  1712.                 curWind = (WindowPtr)(((WindowPeek)curWind)->nextWindow);
  1713.             };
  1714.             if (!curWind) break;
  1715.             curPos.h += offset;
  1716.             curPos.v += offset;
  1717.         };
  1718.         if (!curWind) break;
  1719.         layer++;
  1720.     };
  1721.     SetPort(savedPort);
  1722.     *pos = curPos;
  1723. }
  1724.  
  1725. /*______________________________________________________________________
  1726.  
  1727.     CancelFilter - Command Period Dialog Filter Proc.
  1728.     
  1729.     Entry:    theDialog = pointer to dialog record.
  1730.                 theEvent = pointer to event record.
  1731.     
  1732.     Exit:        if command-period or escape typed:
  1733.     
  1734.                 function result = true.
  1735.                 itemHit = item number of cancel button.
  1736.                 
  1737.                 else if user specified a filterProc on the call to
  1738.                 utl_StopAlert:
  1739.                 
  1740.                 user's filterProc called, function result and itemHit
  1741.                 returned by user's filterProc.
  1742.                 
  1743.                 else:
  1744.                 
  1745.                 function result = false.
  1746.                 itemHit = undefined.
  1747. _____________________________________________________________________*/
  1748.  
  1749.  
  1750. static pascal Boolean CancelFilter (DialogPtr theDialog,
  1751.     EventRecord *theEvent, short *itemHit)
  1752.     
  1753. {
  1754.     short                key;            /* ascii code of key pressed */
  1755.     
  1756.     if (theEvent->what != keyDown && theEvent->what != autoKey) {
  1757.         if (Filter) {
  1758.             return (*Filter)(theDialog, theEvent, itemHit);
  1759.         } else {
  1760.             return false;
  1761.         };
  1762.     } else {
  1763.         key = theEvent->message & charCodeMask;
  1764.         if (key == escapeKey) {
  1765.             *itemHit = CancelItem;
  1766.             utl_FlashButton(theDialog, CancelItem);
  1767.             return true;
  1768.         } else if ((theEvent->modifiers & cmdKey) &&
  1769.             (key == '.')) {
  1770.             *itemHit = CancelItem;
  1771.             utl_FlashButton(theDialog, CancelItem);
  1772.             return true;
  1773.         } else if (Filter) {
  1774.             return (*Filter)(theDialog, theEvent, itemHit);
  1775.         } else if (key == returnKey || key==enterKey) {
  1776.             *itemHit = 1;
  1777.             utl_FlashButton(theDialog, 1);
  1778.             return true;
  1779.         } else {
  1780.             return false;
  1781.         };
  1782.     };
  1783. }
  1784.  
  1785. /*______________________________________________________________________
  1786.  
  1787.     utl_StopAlert - Present Stop Alert
  1788.     
  1789.     Entry:    alertID = resource id of alert.
  1790.                 filterProc = pointer to filter proc.
  1791.                 cancelItem = item number of cancel button, or 0 if
  1792.                     none.
  1793.     
  1794.     Exit:        function result = item number
  1795.                 
  1796.     This routine is identical to the Dialog Manager routine StopAlert,
  1797.     except that it centers the alert on the main window, and if requested,
  1798.     command/period or the Escape key is treated the same as a click on the 
  1799.     Cancel button.
  1800. _____________________________________________________________________*/
  1801.  
  1802.  
  1803. short utl_StopAlert (short alertID, ModalFilterProcPtr filterProc,
  1804.     short cancelItem)
  1805.  
  1806. {
  1807.     Handle            h;                /* handle to alert resource */
  1808.     short                result;        /* function result */
  1809.     
  1810.     h = GetResource('ALRT', alertID);
  1811.     HLock(h);
  1812.     utl_CenterDlogRect(*(Rect**)h, false);
  1813.     if (cancelItem) {
  1814.         Filter = filterProc;
  1815.         CancelItem = cancelItem;
  1816.         result = StopAlert(alertID, CancelFilter);
  1817.     } else {
  1818.         result = StopAlert(alertID, filterProc);
  1819.     };
  1820.     HUnlock(h);
  1821.     return result;
  1822. }
  1823.  
  1824. /*______________________________________________________________________
  1825.  
  1826.     utl_SysHasNotMgr - Check to See if System has Notification Manager.
  1827.     
  1828.     Exit:            function result = true if system has Notification
  1829.                         Manager..
  1830.     
  1831.     The system version must be >= 6.0.
  1832. _____________________________________________________________________*/
  1833.  
  1834.  
  1835. Boolean utl_SysHasNotMgr (void)
  1836.  
  1837. {
  1838.     if (!GotSysEnviron) GetSysEnvirons();
  1839.     return TheWorld.systemVersion >= 0x0600;
  1840. }
  1841.  
  1842. /*______________________________________________________________________
  1843.  
  1844.     utl_SysHasPopUp - Check to See if System has Popup Menus.
  1845.     
  1846.     Exit:            function result = true if system has popup menus.
  1847.     
  1848.     The system version must be >= 4.1, the machine must have the 128 ROM or 
  1849.     later, and the PopUpMenuSelect trap must exist.
  1850. _____________________________________________________________________*/
  1851.  
  1852.  
  1853. Boolean utl_SysHasPopUp (void)
  1854.  
  1855. {
  1856.     if (!GotSysEnviron) GetSysEnvirons();
  1857.     if ((TheWorld.systemVersion < 0x0410) || (TheWorld.machineType == envMac)) {
  1858.         return false;
  1859.     } else {
  1860.         return NGetTrapAddress(_PopUpMenuSelect & 0x3ff, ToolTrap) !=
  1861.             NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap);
  1862.     };
  1863. }
  1864.  
  1865. /*______________________________________________________________________
  1866.  
  1867.     utl_VolIsMFS - Test for MFS volume.
  1868.     
  1869.     Entry:    vRefNum = volume reference number.
  1870.     
  1871.     Exit:        function result = true if volume is MFS.
  1872. _____________________________________________________________________*/
  1873.  
  1874.  
  1875. Boolean utl_VolIsMFS (short vRefNum)
  1876.  
  1877. {
  1878.     HParamBlockRec        vBlock;            /* vol info param block */
  1879.     
  1880.     vBlock.volumeParam.ioNamePtr = nil;
  1881.     vBlock.volumeParam.ioVolIndex = 0;
  1882.     vBlock.volumeParam.ioVRefNum = vRefNum;
  1883.     vBlock.volumeParam.ioVSigWord = 0xd2d7;    /* in case we don't have HFS */
  1884.     (void) PBHGetVInfo(&vBlock, false);
  1885.     return vBlock.volumeParam.ioVSigWord == 0xd2d7;
  1886. }
  1887.  
  1888. /*______________________________________________________________________
  1889.  
  1890.     utl_WaitNextEvent - Get Next Event.
  1891.     
  1892.     Entry:    eventMask = event mask.
  1893.                 sleep = sleep interval.
  1894.                 mouseRgn = mouse region.
  1895.     
  1896.     Exit:        theEvent = the next event.
  1897.                 function result = true if event to be processed.
  1898.                 
  1899.     This routine calls WaitNextEvent if the trap exists, otherwise it
  1900.     calls GetNextEvent.  
  1901.     
  1902.     If GetNextEvent is called, the sleep and mouseRgn parameters are 
  1903.     ignored.  SystemTask is also called.
  1904.                 
  1905.     This routine also saves and restores the current grafport.  This is
  1906.     necessary to protect against some GetNextEvent trap patches which change
  1907.     the grafport without restoring it (e.g., the Flex screen saver).
  1908. _____________________________________________________________________*/
  1909.  
  1910.  
  1911. Boolean utl_WaitNextEvent (short eventMask, EventRecord *theEvent,
  1912.     long sleep, RgnHandle mouseRgn)
  1913.  
  1914. {
  1915.     GrafPtr            curPort;        /* pointer to current grafport */
  1916.     Boolean            result;        /* function result */
  1917.     static short    wne = 2;        /* 0 if WaitNextEvent does not exist
  1918.                                             1 if WaitNextEvent exists
  1919.                                             2 if we don't yet know (first call) */
  1920.     
  1921.     /* Find out whether the WaitNextEvent trap is implemented if this is 
  1922.         the first call. */
  1923.     
  1924.     if (wne == 2) {
  1925.         if (!GotSysEnviron) GetSysEnvirons();
  1926.         if (TheWorld.machineType < 0) {
  1927.             wne = false;
  1928.         } else {
  1929.             wne = (NGetTrapAddress(_WaitNextEvent & 0x3ff, ToolTrap) == 
  1930.                 NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap)) ? 0 : 1;
  1931.         };
  1932.     };
  1933.     
  1934.     /* Save the port, call the trap, and restore the port. */
  1935.     
  1936.     GetPort(&curPort);
  1937.     if (wne) {
  1938.         result = WaitNextEvent(eventMask, theEvent, sleep, mouseRgn);
  1939.     } else {
  1940.         SystemTask();
  1941.         result = GetNextEvent(eventMask, theEvent);
  1942.     };
  1943.     SetPort(curPort);
  1944.     return result;
  1945. }
  1946.  
  1947. /*______________________________________________________________________
  1948.  
  1949.     misc_ValPrint - Validate Print Record.
  1950.     
  1951.     Entry:    p = pointer to rpp param block.
  1952.                 p->hPrint = pointer to print record, or nil if none
  1953.                     yet allocated.
  1954.                 report = true if this is for a report printing operation.
  1955.                 report = false if this is for a document printing operation.
  1956.     
  1957.     Exit:        p->hPrint = pointer to print record.  A new one is allocated
  1958.                     if necessary, and any existing one is validated.
  1959.                     
  1960.                 If a new print record is allocated, or if the existing one
  1961.                 was invalid, then the other fields in the rpp param block
  1962.                 are also initialized.  The initial values depend on whether
  1963.                 this is a report or document being printed, and whether
  1964.                 the output device is a LaserWriter or ImageWriter.
  1965. _____________________________________________________________________*/
  1966.  
  1967.  
  1968. void misc_ValPrint (rpp_PrtBlock *p, Boolean report)
  1969.  
  1970. {
  1971.     Boolean                setDefaults;    /* true to set default values */
  1972.     Boolean                laser;            /* true if laserwriter */
  1973.     char                    *defFont1;        /* pointer to first default font name */
  1974.     char                    *defFont2;        /* pointer to second default font name */
  1975.     short                    defSize1;        /* first default size */
  1976.     short                    defSize2;        /* second default size */
  1977.  
  1978.     setDefaults = true;
  1979.     if (p->hPrint) {
  1980.         setDefaults = PrValidate(p->hPrint);
  1981.     } else {
  1982.         p->hPrint = (THPrint)NewHandle(sizeof(TPrint));
  1983.         PrintDefault(p->hPrint);
  1984.         defSize1 = (**(p->hPrint)).prInfo.iVRes;
  1985.     };
  1986.     if (setDefaults) {
  1987.         laser = utl_IsLaser(p->hPrint);
  1988.         if (report) {
  1989.             if (laser) {
  1990.                 defFont1 = defFont2 = "\pHelvetica";
  1991.             } else {
  1992.                 defFont1 = defFont2 = "\pGeneva";
  1993.             };
  1994.             defSize1 = defSize2 = 10;
  1995.         } else {
  1996.             if (laser) {
  1997.                 defFont1 = "\pPalatino";
  1998.                 defSize1 = 10;
  1999.                 defFont2 = "\pTimes";
  2000.                 defSize2 = 12;
  2001.             } else {
  2002.                 defFont1 = defFont2 = "\pGeneva";
  2003.                 defSize1 = defSize2 = 10;
  2004.             };
  2005.         };
  2006.         p->reverseOrder = laser ? true : false;
  2007.         if (utl_GetFontNumber(defFont1, &p->fontNum)) {
  2008.             p->fontSize = defSize1;
  2009.         } else if (utl_GetFontNumber(defFont2, &p->fontNum)) {
  2010.             p->fontSize = defSize2;
  2011.         } else {
  2012.             p->fontNum = applFont;
  2013.             p->fontSize = defSize1;
  2014.         };
  2015.         p->topMargin = p->botMargin = 50;
  2016.         p->leftMargin = p->rightMargin = 100;
  2017.     };
  2018.     p->header = true;
  2019.     p->titleSep = report ? 25 : 40;
  2020.     p->titleFont = p->fontNum;
  2021.     p->titleStyle = bold;
  2022.     p->titleSize = 90;
  2023.     p->dlogID = prDlogID;
  2024.     p->tabConID = tconID;
  2025.     p->emptyPageRangeID = noPagesID;
  2026.     p->ditlID = pageSetupID;
  2027.     p->sizeRangeID = illegalSizeID;
  2028.     p->marginsTooBigID = marTooBigID;
  2029.     p->truncateRightID = truncRightID;
  2030.     p->truncateBottomID = truncBottomID;
  2031.     p->minFontSize = 1;
  2032.     p->maxFontSize = 24;
  2033.     p->updateAll = nil;
  2034. }
  2035.  
  2036.  
  2037.